public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, restaurant, playground, lab, office, gymnasium, theater,endroom,library,classroom,canteen;
        // create the rooms
        outside = new Room("校内");
        restaurant = new Room("餐厅",new Thing("牛奶",20));
        canteen = new Room("食堂",new Thing("烤鸡",50));
        playground = new Room("操场",new Thing("宿舍大门钥匙",150));
        lab = new Room("实验室",new Thing("药丸",-100));
        office = new Room("办公室");
        gymnasium = new Room("体育馆",new Thing("棒球棒",100));
        theater = new Room("剧院",new Thing("道具盔甲",70));
        endroom = new Room("宿舍",new Thing("床",10000));
        library = new Room("图书馆");
        classroom = new Room("教室",new Thing("一书本",1000));
        outside.setExit("east",lab);
        outside.setExit("south",office);
        outside.setExit("west",gymnasium);
        outside.setExit("north",canteen);
        
        office.setExit("north",outside);
        
        lab.setExit("west",outside);
        
        gymnasium.setExit("west",theater);
        gymnasium.setExit("north",endroom);
        gymnasium.setExit("east",outside);
        
        theater.setExit("north",library);
        theater.setExit("east",gymnasium);
        
        library.setExit("east",endroom);
        library.setExit("south",theater);
        
        endroom.setExit("east",canteen);
        endroom.setExit("west",library);
        endroom.setExit("north",classroom);
        endroom.setExit("south",gymnasium);
        
        canteen.setExit("west",endroom);
        canteen.setExit("north",playground);
        canteen.setExit("south",outside);
        canteen.setExit("up",restaurant);
        
        restaurant.setExit("down",canteen);
        
        playground.setExit("south",canteen);
        playground.setExit("west",classroom);
        
        classroom.setExit("east",playground);
        classroom.setExit("south",endroom);
        
        currentRoom = outside;
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() throws Exception
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("Thank you for playing.  Good bye a.");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎来到模拟人生游戏!");
        System.out.println("你将随机进入其他人的人生，并最好度过难关");
        System.out.println("建议您先使用 ”help“ 来查看下现在自己的处境呢");
        System.out.println();
        System.out.println("你在" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) throws Exception
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("I don't know what you mean...");
            return false;
        }

        Word commandWord = command.getCommandWord();
        switch(commandWord){
            case HELP:
                printHelp();
                break;
            case GO:
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case LOOK:
                looks();
                break;
            case PICK:
                picks();
                break;
            case EAT:
                eats(command);
                break;
            case CHECK:
                checks();
                break;
        }
        
        return wantToQuit;
    }

    // implementations of user commands:

    
    
    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() throws Exception
    {
        System.out.println("茫茫夜色中，刚上学没几天的你梦游到了校内某处");
        System.out.println("太黑了，你看不太清各个方向是什么房间");
        System.out.println("你还是个新生也没记住各个地点的位置");
        System.out.println("现在的你十分的累，只想回到宿舍躺床上好好睡一觉");
        Thread.sleep(2000);
        System.out.println("");
        System.out.println("但你要注意，现在这个时间回去的你已经违反了校规，");
        System.out.println("可不要被抓到啊。");
        Thread.sleep(2000);
        System.out.println("Your command words are:");
        System.out.println("   "+ CommandWords.allCommandWord());
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    Player t=new Player();
    private void goRoom(Command command) throws Exception
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪 ?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goRoom(direction);
        
         int a=0,b=0,c=0,d=0;
        if (nextRoom == null) {
            System.out.println("你是想撞墙么！!");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你走进了" + currentRoom.getDescription());
            currentRoom.printExits();
            t.step();
            if(currentRoom.getDescription().equals("宿舍")){
                for(String key : t.bag.keySet()){
                    if(key.equals("宿舍大门钥匙")){
                        a=1;
                    }
                }
                if(a==1){
                    System.out.println("你到达了宿舍并用钥匙打开了门 ");
                    System.out.println("又看到了那熟悉的床铺");
                    Thread.sleep(2000);
                    System.out.println("你终于可以躺床上好好休息一下了.");
                    Thread.sleep(2000);
                    System.out.println("晚安，好梦.");
                    Thread.sleep(2000);
                    System.out.println(" ");
                    System.out.println(" ");
                    System.out.println(" ");
                    System.out.println("恭喜你通过了模拟人生中的困境");
                    Thread.sleep(1000);
                    System.out.println("愿你的人生也这般成功！！！！！");
                    System.exit(1);
                }
                else{
                    System.out.println("你到了宿舍但门却是锁着的");
                    System.out.println("听说学校的小混混偷偷配了一套钥匙");
                    System.out.println("但常人可打不过他，你最好有武器和防具");
                }
            }
            if(currentRoom.getDescription().equals("操场")){
                for(String key : t.bag.keySet()){
                    if(key.equals("棒球棒")){
                        c=1;
                    }
                    if(key.equals("道具盔甲")){
                        d=1;
                    }
                }
                if(c==1&&d==1)
                {
                    System.out.println("你看见了个小混混，他也发现了你");
                    Thread.sleep(2000);
                    System.out.println("但你手握棒球棒，丝毫不慌，打了小混混一棍，小混混差点晕倒");
                    Thread.sleep(2000);
                    System.out.println("你被打中一拳，但因为道具铠甲护体，屁事没有");
                    Thread.sleep(2000);
                    System.out.println("你又反手一棒直逼小混混脑门，小混混直接倒地不起");
                    Thread.sleep(2000);
                    System.out.println("小混混衣服中好像有什么东西掉了出来");
                    Thread.sleep(2000);
                }
                else{
                    System.out.println("你看见了个小混混，他也发现了你");
                    Thread.sleep(2000);
                    System.out.println("你赤手空拳有那么一丝慌张，打了混混一拳，但他屁事没有");
                    Thread.sleep(2000);
                    System.out.println("接着你被小混混一套连招打的无力招架");
                    Thread.sleep(2000);
                    System.out.println("你还是撑不住倒地了");
                    Thread.sleep(2000);
                    System.out.println(" ");
                    System.out.println("你因为夜不归宿和打架斗殴被开除，诶");
                    Thread.sleep(2000);
                    System.out.println("请以后一定要好好学习啊！！！！");
                    Thread.sleep(2000);
                    System.out.println(" ");
                    System.out.println(" ");
                    System.out.println(" ");
                    System.out.println("因为你的鲁莽，模拟人生游戏结束");
                    System.exit(1);
                }
            }
                
        }
    }
    
    
    
    private void eats(Command command){
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("吃什么????");
            return;
        }
        String foot = command.getSecondWord();
        t.eat(foot);
    }
    
    private void looks(){
        System.out.println(currentRoom.getThing());
    }
    
    
    private void picks(){
        t.pick(currentRoom.thing);
        currentRoom.removeThing();
    }
    
    private void checks(){
        t.checkss();
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}
